package com.lhkj.ct.meta.modules.warning.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.constants.DictConstants;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.mybatis.core.metadata.SqlOrder;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.base.utils.file.word.PoiWordUtils;
import com.lhkj.ct.meta.admin.service.SysDictDataService;
import com.lhkj.ct.meta.modules.patient.mapper.PatientMapper;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatient;
import com.lhkj.ct.meta.modules.report.BaseFactory;
import com.lhkj.ct.meta.modules.report.word.CounselingHandle;
import com.lhkj.ct.meta.modules.warning.mapper.TblPsyConsRecordMapper;
import com.lhkj.ct.meta.modules.warning.model.entity.TblPsyConsRecord;
import com.lhkj.ct.meta.modules.warning.model.vo.PsyConsRecordVo;
import com.lhkj.ct.meta.modules.warning.service.PsyConsRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Map;

/**
 * <p>
 * 心理咨询记录 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-10-30
 */
@Service
public class PsyConsRecordServiceImpl extends IServiceImpl<TblPsyConsRecordMapper, TblPsyConsRecord> implements PsyConsRecordService {

    @Resource
    private PatientMapper patientMapper;

    @Resource
    private SysDictDataService dictDataService;

    @Override
    public IPage<TblPsyConsRecord> selectPages(TblPsyConsRecord consRecords, PageQuery pageQuery) {
        return getBaseMapper().selectPages(
                generatePage(pageQuery, SqlOrder.desc(TblPsyConsRecord::getCreateTime, "t")),
                consRecords,
                pageQuery
        );
    }

    @Override
    public IPage<TblPsyConsRecord> selectHistoriesByUser(String patId, PageQuery pageQuery) {
        return getBaseMapper().selectHistoriesByUser( generatePage(pageQuery, SqlOrder.desc(TblPsyConsRecord::getCreateTime)), patId);
    }

    @Override
    public TblPsyConsRecord getById(Serializable id) {
        return getBaseMapper().getById(id);
    }

    @Override
    public int insert(TblPsyConsRecord entity) {
        Asserts.assertTrue(dictDataService.validated(DictConstants.PSY_QUESTION_TYPE, entity.getType()), "问题类型不合法！");
        TblPatient patient = patientMapper.getReportUserInfo(entity.getPatId());
        Validate.notNull(patient, "档案不存在或已删除！");
        entity.setPatName(patient.getPatName());
        entity.setPatSex(patient.getPatSex());
        entity.setPatAge(patient.getPatAge());
        entity.setPatBirthday(patient.getPatBirthday());
        entity.setPatMobile(patient.getPatMobile());
        return getBaseMapper().insert(entity);
    }

    @Override
    public int updateById(TblPsyConsRecord entity) {
        TblPsyConsRecord record = getBaseMapper().selectById(entity);
        Validate.notNull(record, "心理咨询记录不存在或已删除！");
        Validate.isTrue(StringUtils.equals(ShiroUtil.getAuthUserInfo().getUserId(), record.getCreateBy()), "心理治疗记录仅限执行人可修改！");
        Validate.isTrue(dictDataService.validated(DictConstants.PSY_QUESTION_TYPE, entity.getType()), "问题类型不合法！");
        entity.setPatName(record.getPatName());
        entity.setPatSex(record.getPatSex());
        entity.setPatAge(record.getPatAge());
        entity.setPatBirthday(record.getPatBirthday());
        entity.setPatMobile(record.getPatMobile());
        return getBaseMapper().updateById(entity);
    }

    @Override
    public PsyConsRecordVo get(String id) {
        PsyConsRecordVo reportInfo = getBaseMapper().getReportInfo(id);
        Validate.notNull(reportInfo, "心理咨询记录不存在或已删除！");
        return reportInfo;
    }

    @Override
    public void exportPdf(String id) {
        PsyConsRecordVo reportInfo = get(id);
        Map<String, Object> data = BeanUtil.beanToMap(reportInfo);
        Document document = BaseFactory.getReport(new CounselingHandle(), data);
        PoiWordUtils.download(document, StringUtils.defaultIfBlank(reportInfo.getPatName(), LocalDate.now().toString())+".pdf", SaveFormat.PDF);
    }
}
